/**
 * 
 */

/**
 * @author antonio081014
 * @since Nov 29, 2011, 4:23:01 AM
 */
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class DropCoins {

    public int getMinimum(String[] board, int K) {
	int N = board.length;
	int M = board[0].length();
	int ret = Integer.MAX_VALUE;
	for (int x = 0; x < N; x++) {
	    for (int y = 0; y < M; y++) {
		for (int xx = x + 1; xx <= N; xx++) {
		    for (int yy = y + 1; yy <= M; yy++) {
			int count = 0;
			for (int i = x; i < xx; i++) {
			    for (int j = y; j < yy; j++) {
				if (board[i].charAt(j) == 'o')
				    count++;
			    }
			}
			if (count == K) {
			    int sum = 2 * Math.min(x, N - xx)
				    + Math.max(x, N - xx) + 2
				    * Math.min(y, M - yy) + Math.max(y, M - yy);
			    ret = Math.min(ret, sum);
			}

		    }
		}
	    }
	}
	if (ret == Integer.MAX_VALUE)
	    return -1;
	return ret;
    }
     public static void main(String[] args)
	{
		long time;
		int answer;
		boolean errors = false;
		int desiredAnswer;
		
		
		time = System.currentTimeMillis();
		answer = new DropCoins().getMinimum(new String[]{".o..","oooo","..o."}, 3);
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 2;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new DropCoins().getMinimum(new String[]{".....o","......","oooooo","oooooo","......","o....."}, 12);
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 3;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new DropCoins().getMinimum(new String[]{"....",".oo.",".oo.","...."}, 3);
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = -1;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new DropCoins().getMinimum(new String[]{".......","..ooo..","ooooooo",".oo.oo.","oo...oo"}, 12);
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 4;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		time = System.currentTimeMillis();
		answer = new DropCoins().getMinimum(new String[]{".................",".ooooooo...oooo..",".ooooooo..oooooo.",".oo.......oo..oo.",".oo.......oo..oo.",".ooooo.....oooo..",".ooooooo...oooo..",".....ooo..oo..oo.","......oo..oo..oo.",".ooooooo..oooooo.",".oooooo....oooo..","................."}, 58);
		System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
		desiredAnswer = 6;
		System.out.println("Your answer:");
		System.out.println("\t" + answer);
		System.out.println("Desired answer:");
		System.out.println("\t" + desiredAnswer);
		if (answer != desiredAnswer)
		{
			errors = true;
			System.out.println("DOESN'T MATCH!!!!");
		}
		else
			System.out.println("Match :-)");
		System.out.println();
		
		
		if (errors)
			System.out.println("Some of the test cases had errors :-(");
		else
			System.out.println("You're a stud (at least on the test data)! :-D ");
	}

}
// Powered by [KawigiEdit] 2.0!
